package com.batchaps.classes;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Sprite2D {

	private Image[] _sprite;
	private int _nSprite;
	private int _iSprite;
	
	private float _posX;
	private float _posY;

	private float _sizeX;
	private float _sizeY;

	private float _velocityX;
	private float _velocityY;

	private int _width;
	private int _height;

	private boolean _visible;
	private boolean _isHover;

	public Sprite2D(String sp , int nSp , String strType){
		_sprite = new Image[nSp];
		_nSprite = nSp;
		_iSprite = 0;
		for(int i = 0 ; i < nSp ;i++){
			try {
				_sprite[i] = Image.createImage(sp + i + strType);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		_width = _sprite[0].getWidth();
		_height = _sprite[0].getHeight();
		_sizeX = 0;
		_sizeY = 0;
		_posX = 0;
		_posY = 0;
		_velocityX = 0;
		_velocityY = 0;
		_visible = true;
		_isHover = false;
	}
	
	public Sprite2D(Image[] sp, int w, int h , float sX , float sY , float posX ,float posY) {
		_sprite = sp;
		_iSprite = 0;
		_nSprite = sp.length;
		_width = w;
		_height = h;
		_sizeX = sX;
		_sizeY = sY;
		_posX = posX;
		_posY = posY;
		_velocityX = 0;
		_velocityY = 0;
		_visible = true;
		_isHover = false;
	}
	
	public Sprite2D(String sp , int nSp  , String strType , int w , int h , float sX , float sY , float posX ,float posY){
		_sprite = new Image[nSp];
		_nSprite = nSp;
		_iSprite = 0;
		for(int i = 0 ; i < nSp ;i++){
			try {
				_sprite[i] = Image.createImage(sp + i + strType);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		_width = w;
		_height = h;
		_sizeX = sX;
		_sizeY = sY;
		_posX = posX;
		_posY = posY;
		_velocityX = 0;
		_velocityY = 0;
		_visible = true;
		_isHover = false;
	}

	public void Draw(Graphics g) {
		if (_sizeX == 0 || _sizeY == 0) {
			g.drawImage(_sprite[_iSprite], (int) _posX, (int) _posY, 0);
		}
	}

	public void setPosition(float x, float y) {
		_posX = x - _width / 2;
		_posY = y - _height / 2;
	}

	public void Move(float x, float y) {
		_posX += x;
		_posY += y;
	}
	
	public void SetFrame(int idx){
		_iSprite = idx;
	}
	
	public void NextFrame(){
		_iSprite = (_iSprite++ % _nSprite);
	}
	
	public boolean CollidesWith(Sprite2D sprite)
    {
        if (_visible && sprite._visible)
        {
            float redundantW1 = _width * 20 / 100;
            float redundantH1 = _height * 20 / 100;
            float redundantW2 = sprite._width * 20 / 100;
            float redundantH2 = sprite._height * 20 / 100;
            float top1 = _posX + redundantH1;
            float bottom1 = _posY + _height - redundantH1;
            float left1 = _posX + redundantW1;
            float right1 = _posX + _width - redundantW1;
            float top2 = sprite._posY + redundantH2;
            float bottom2 = sprite._posY + sprite._height - redundantH2;
            float left2 = sprite._posX + redundantW2;
            float right2 = sprite._posX + sprite._width - redundantW2;
            if (bottom1 >= top2 && top1 <= bottom2 && left1 <= right2 && right1 >= left2)
                return true;
        }
        return false;
    }
	
    public boolean IsMouseInteract(int x, int y)
    {
        if (_posX <= x && _posX + _width >= x && _posY <= y && _posY + _height >= y && _visible)
        {
            return true;
        }
        return false;
    }
    
    public float getX(){
    	return _posX + _width/2;
    }
    
    public float getY(){
    	return _posY + _height / 2;
    }

	public Image[] get_sprite() {
		return _sprite;
	}

	public void set_sprite(Image[] _sprite) {
		this._sprite = _sprite;
	}

	public int get_nSprite() {
		return _nSprite;
	}

	public void set_nSprite(int _nSprite) {
		this._nSprite = _nSprite;
	}

	public float get_posX() {
		return _posX;
	}

	public void set_posX(float _posX) {
		this._posX = _posX;
	}

	public float get_posY() {
		return _posY;
	}

	public void set_posY(float _posY) {
		this._posY = _posY;
	}

	public float get_sizeX() {
		return _sizeX;
	}

	public void set_sizeX(float _sizeX) {
		this._sizeX = _sizeX;
	}

	public float get_sizeY() {
		return _sizeY;
	}

	public void set_sizeY(float _sizeY) {
		this._sizeY = _sizeY;
	}

	public float get_velocityX() {
		return _velocityX;
	}

	public void set_velocityX(float _velocityX) {
		this._velocityX = _velocityX;
	}

	public float get_velocityY() {
		return _velocityY;
	}

	public void set_velocityY(float _velocityY) {
		this._velocityY = _velocityY;
	}

	public int get_width() {
		return _width;
	}

	public void set_width(int _width) {
		this._width = _width;
	}

	public int get_height() {
		return _height;
	}

	public void set_height(int _height) {
		this._height = _height;
	}

	public boolean is_visible() {
		return _visible;
	}

	public void set_visible(boolean _visible) {
		this._visible = _visible;
	}

	public boolean is_isHover() {
		return _isHover;
	}

	public void set_isHover(boolean _isHover) {
		this._isHover = _isHover;
	}

}
